package homework.handler.session;

import com.sun.net.httpserver.HttpExchange;
import homework.RequestMethod;
import homework.Resource;
import homework.handler.AbstractRequestHandler;
import homework.manager.SessionManager;

import java.io.IOException;

@RequestMethod("GET")
public class SessionHandler extends AbstractRequestHandler<SessionRequest> {
    @Resource
    private SessionManager sessionManager;

    @Override
    public SessionRequest parse(HttpExchange exchange, String[] paths) {
        boolean match = paths.length > 2 && paths[2].equals("session");
        if (match) {
            SessionRequest sessionRequest = new SessionRequest();
            sessionRequest.setCustomerId(paths[1]);
            return sessionRequest;
        } else {
            return null;
        }
    }

    @Override
    public void handle(HttpExchange exchange, SessionRequest request) throws IOException {
        response(exchange, sessionManager.getSessionKey(request.getCustomerId()));
    }
}
